package henry.lucky.alphay;

import henry.lucky.alphay.Domain.Prize;
import henry.lucky.alphay.db.DataBase;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Settings{

	private DataBase base=new DataBase();	
	private DefaultTableModel model=null;	
	String[] colNamePrize;
	String[][] rowDataPrize;
	JTable tablePrize;
	JFrame frame;
	public Settings() {
		super();				
	}

	public void showSettings(){
		//建立界面
		frame=new JFrame("奖项设置");
		frame.setSize(800, 600);
		frame.setVisible(true);
		//设置布局
		Container container=frame.getContentPane();
		JPanel panelTop=new JPanel();		
		JPanel panelCenterPrize=new JPanel();
		panelCenterPrize.setLayout(new BorderLayout());
		JPanel panelPrizeDown=new JPanel(new FlowLayout());
		container.add("North", panelTop);
		container.add("Center",panelCenterPrize);
		container.add("South",panelPrizeDown);
		//设置表头		
		JLabel labelTop=new JLabel("奖项设置");
		Font font=new Font("title", Font.CENTER_BASELINE, 40);
		labelTop.setFont(font);
		panelTop.add(labelTop);
		//设置显示区域
		List<Prize> prizes=base.findCurrentLuckyPrize();
		colNamePrize=new String[]{"编号","奖项","奖品","抽奖人数","抽奖次数","价值","抽奖人员","抽奖顺序","是否抽过奖"};		
		rowDataPrize = new String[prizes.size()][9];
		int row=0;
		for(Prize prize:prizes){
			rowDataPrize[row][0]=String.valueOf(prize.getId());
			rowDataPrize[row][1]=prize.getPrizeName();
			rowDataPrize[row][2]=prize.getName();
			rowDataPrize[row][3]=String.valueOf(prize.getNumber());
			rowDataPrize[row][4]=String.valueOf(prize.getCycleIndex());
			rowDataPrize[row][5]=String.valueOf(prize.getPrice());
			rowDataPrize[row][6]=prize.getPerson();
			rowDataPrize[row][7]=String.valueOf(prize.getLuckyIndex());
			if(prize.getCanType()==0){
				rowDataPrize[row][8]="未抽奖";
			}else{
				rowDataPrize[row][8]="已抽奖";
			}	
			row++;
		}
		model=new DefaultTableModel(rowDataPrize, colNamePrize);
		tablePrize=new JTable(model);
		panelCenterPrize.add(new JScrollPane(tablePrize),BorderLayout.CENTER);	
		//设置操作按钮控件
		JButton buttonInit=new JButton("系统初始化");
		JButton addButton=new JButton("添加");
		JButton editButton=new JButton("修改");
		JButton delButton=new JButton("删除");		
		panelPrizeDown.add(addButton);
		panelPrizeDown.add(editButton);
		panelPrizeDown.add(delButton);
		panelPrizeDown.add(buttonInit);
		buttonInit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				int init=JOptionPane.showConfirmDialog(null, "你确认要初始化抽奖内容，此操作将清空所有的抽奖信息");
				if(init==0){
					
					int flag=base.initLucky();
					if(flag==1){
						JOptionPane.showMessageDialog(null, "初始化抽奖成功");
					}else{
						JOptionPane.showMessageDialog(null, "初始化抽奖失败");
					}
				}
				refreshTable();
			}
		});	
		addButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				PrizeSettings settings=new PrizeSettings(frame);
				settings.setModal(true);
				settings.showPrizeSettings();
				settings.setSize(300, 384);
				settings.setVisible(true);
				refreshTable();
			}
		});
		editButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				int row=tablePrize.getSelectedRow();
				String canTypeStr=(String) tablePrize.getValueAt(row, 8);
				int cantype=1;
				if("已抽奖".equals(canTypeStr)){
					cantype=0;
				}
				int id=Integer.valueOf((String)tablePrize.getValueAt(row, 0));
				int number=Integer.valueOf((String)tablePrize.getValueAt(row, 3));
				int cycleIndex=Integer.valueOf((String)tablePrize.getValueAt(row, 4));;
				int price=Integer.valueOf((String)tablePrize.getValueAt(row, 5));
				int luckyIndex=Integer.valueOf((String)tablePrize.getValueAt(row, 7));
				Prize prize=new Prize(id, (String)tablePrize.getValueAt(row, 1), (String)tablePrize.getValueAt(row, 2), number, cycleIndex, price, (String)tablePrize.getValueAt(row, 6), luckyIndex,cantype);
				PrizeSettings settings=new PrizeSettings(frame,prize);
				settings.setModal(true);
				settings.showPrizeSettings();
				settings.setSize(300, 384);
				settings.setVisible(true);
				refreshTable();
			}
		});
		delButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int[] rows=tablePrize.getSelectedRows();
				for(int i=0;i<rows.length;i++){
					String idStr=(String) tablePrize.getValueAt(rows[i], 0);
					int id=Integer.valueOf(idStr);
					base.deletePrize(id);
				}
				refreshTable();
			}
		});
	}
	private void refreshTable(){
		
		model.getDataVector().removeAllElements();
		List<Prize> prizes=base.findCurrentLuckyPrize();
		String[] data=new String[9];
		for(Prize prize:prizes){
			data[0]=String.valueOf(prize.getId());
			data[1]=prize.getPrizeName();
			data[2]=prize.getName();
			data[3]=String.valueOf(prize.getNumber());
			data[4]=String.valueOf(prize.getCycleIndex());
			data[5]=String.valueOf(prize.getPrice());
			data[6]=prize.getPerson();
			data[7]=String.valueOf(prize.getLuckyIndex());
			if(prize.getCanType()==0){
				data[8]="未抽奖";
			}else{
				data[8]="已抽奖";
			}	
			model.addRow(data);
		}
		tablePrize.revalidate();
	}
}
